const state = {
  typePage: {},
  typeList: [],
  list: [],
  page: {}
}

const actions = {
  saveTypes: ({ commit }, payload) => {
    commit('SAVETYPES', payload)
  },
  saveList: ({ commit}, payload) => {
    commit('SAVELIST', payload)
  }
}

const mutations = {
  SAVETYPES: (state, payload) => {
    state.typeList = payload.result,
    state.typePage = {
      size: 10,
      number: payload.number || 1,
      total: payload.resultNum
    }
  },
  SAVELIST: (state, payload) => {
    if (payload.result.length === 0) {
      state.list = []
    } else {
        state.list = payload.result.map(item => {
            return {
                id: item.id,
                publicAt: item.public_at,
                title: item.title,
                subTitle: item.sub_title,
                isRecommend: item.is_recommend,
                content: item.content,
                typeId: item.type_id,
                origin: item.origin,
                author: item.author,
                state: item.state,
                description: item.description,
                clickTimes: item.click_times
            }
        })
    }
    state.page = {
      size: 10,
      number: payload.number || 1,
      total: payload.resultNum
    }
  }
}

export default {
  state,
  actions,
  mutations
}